<template>
  <el-card>
    <el-button @click="$router.push('/categroy/Add')" type="primary" icon="el-icon-circle-plus-outline"
      >新增</el-button
    >
    <el-divider />
    <el-table
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID"  min-width="80">
      </el-table-column>
      <el-table-column prop="catename" label="分类名称"  min-width="180">
      </el-table-column>
      <el-table-column prop="img" label="分类图片"  min-width="180">
          <template v-slot="scope">
              <img :src="$imgBaseURL+scope.row.img" alt="" style="height:120px">
          </template>
      </el-table-column>
      <el-table-column prop="status" label="状态"> 
          <template v-slot="scoped" min-width="180">
            <el-tag type="success" v-if="scoped.row.status==1">正常</el-tag>
            <el-tag type="danger" v-else>正常</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="$router.push('/categroy/edit/'+scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="del(scope.row.id)"></el-button>
          </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>

import { getCateList } from "../../request/categroy";
export default {
  data() {
    return {
      list: [],
    };
  },
  // 在生命周期中调用methods的方法
  created() {
    this.getCateList();
  },
  methods: {
    // 删除
    del(id){ 
      this.$confirm(async ()=>{
        const res=await this.$http.post('/api/catedelete',{id});
        if(res.code===200){
          this.$success('删除成功')
          // 删除页面中的数据
          // const index=this.list.(item=>item.id==findIndex=id);
           // this.list.splice(index,1);
          this.getCateList()
        }
      })
    },
    // async和await 以同步的流程展现异步
    // 这个getCateList是一个方法
    async getCateList() {
      // 更新数据       这个getCateList是从接口引入的数据
      const list = await getCateList();
      // 如果拿到数据
      if (list) {
        // 就更新数据  往数组list里添加数据
        this.list = list;
      }
    },
  },
};
</script>

<style>
</style>